﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Orange.Logistics.Infrastructure;
using Orange.Logistics.Models;
using Orange.Logistics.Services;
using System;
using Newtonsoft.Json;

namespace Orange.Logistics.Controllers {

    public class GroupController : BaseController<Group> {

        private readonly IGroupService service;

        public GroupController(IGroupService service)
            : base(service) {
            this.service = service;
        }

        //Get group list.
        public JsonResult GetGroups() {
            var groups = service.GetGroups();
            return Json(new { Rows = groups, Total = groups.Count() }, JsonRequestBehavior.AllowGet);
        }

        //Init liger_tree.
        public JsonResult GetTree() {
            var groups = service.GetGroups();
            List<TreeNode> root = new List<TreeNode>();
            root.Add(new TreeNode {
                id = 0, text = "全部分组", children = groups.Select(g => new TreeNode { id = g.Groupid, text = g.Groupname })
            });
            return Json(root, JsonRequestBehavior.AllowGet);
        }

        #region Memeber Actions

        public JsonResult GetMembers(int id) {
            InitPager();
            var source = service.GetMemebers(id);
            int count = source.Count();
            var rows = PagedList<Customer>(source).ToList();
            return Json(new { Rows = rows, Total = count }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetNoMembers(int id) {
            InitPager();
            var source = service.GetNoMemebers(id);
            int count = source.Count();
            var rows = PagedList<Customer>(source).ToList();
            return Json(new { Rows = rows, Total = count }, JsonRequestBehavior.AllowGet);
        }

        //Select memebers.
        public ActionResult MemberList() {
            return View();
        }

        [HttpPost]
        public JsonResult AddMembers() {
            try {
                int groupid = Convert.ToInt32(Request["groupid"].ToString());
                string curUser = CurrentUser.UserId;
                DateTime curtime = DateTime.Now;
                List<Customer> models = JsonConvert.DeserializeObject<List<Customer>>(Request["data"]);
                foreach (var c in models) {
                    service.AddMember(new CustomerGroup {
                        CreateTime = curtime,
                        CreateUser = curUser,
                        Customerid = c.CustomerId,
                        Groupid = groupid
                    });
                }
                service.SaveChanges();
                return Json(true);
            } catch { }
            return Json(false);
        }

        [HttpPost]
        public JsonResult DeleteMembers() {
            try {
                int groupid = Convert.ToInt32(Request["groupid"].ToString());
                string curUser = CurrentUser.UserId;
                DateTime curtime = DateTime.Now;
                List<Customer> models = JsonConvert.DeserializeObject<List<Customer>>(Request["data"]);
                service.DeleteMemebers(groupid, models);
                service.SaveChanges();
                return Json(true);
            } catch { }
            return Json(false);
        }
        #endregion

        protected override Func<Group, object> GetOrderBy(string sort) {
            return (Group g) => {
                switch (sort) {
                    default:
                        return g.Groupid;
                }
            };
        }

        protected override void Dispose(bool disposing) {
            if (disposing) {
                service.Dispose();
            }
        }
    }
}